<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="试卷" prop="fileName">
        <el-input v-model="queryParams.fileName" placeholder="请输入试卷" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['datacenter:filehead:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['datacenter:filehead:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fileheadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="试卷" align="center" prop="fileName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleInfoDetailList(scope.row)"
           >查看单词</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['datacenter:filehead:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单词统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="试卷" prop="fileName">
          <el-upload ref="fileUpload" action="" :on-change="beforeUpload" drag
            :show-file-list="true"
            :file-list="fileList"
            :limit="1"
            :auto-upload="false">
            <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处</div>
              <div class="el-upload__tip" slot="tip">只能上传docx/pdf文件</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="isSubmitDisabled">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  <fileListBody :open="detailFlag" :fileId="fileId" @close-select-window="closeDetailWindow()"/>
  </div>
</template>

<script>
import { listFilehead, delFilehead, addFilehead } from "@/api/bus_web/datacenter/filehead";
import fileListBody from "./file_body_liest.vue"

export default {
  name: "Filehead",
  components:{fileListBody},
  data() {
    return {
      // 遮罩层
      loading: true,
      fileId:null,
      detailFlag:false,
      isSubmitDisabled:false,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单词统计表格数据
      fileheadList: [],
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
      },
      fileType: ["docx","pdf"],
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleInfoDetailList(row){
        this.detailFlag=true
        this.fileId=row.id
    },
    closeDetailWindow(){
      this.detailFlag=false
      this.fileId=null
    },
    /** 查询单词统计列表 */
    getList() {
      this.loading = true;
      listFilehead(this.queryParams).then(response => {
        this.fileheadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fileName: null,
        deleted: null,
        createBy: null,
        updateBy: null,
        gmtCreate: null,
        gmtModify: null
      };
      this.fileList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "上传试卷";
      this.isSubmitDisabled=false;
      this.fileList=[];
    },
    /** 提交按钮 */
    submitForm() {
      if(this.fileList.length<1){
                this.$modal.msgError(`请选择试卷文件`);
                return;

      }
      this.isSubmitDisabled=true
      const formData = new FormData();
      formData.append('file', this.fileList[0].raw);
      this.$refs["form"].validate(valid => {
        if (valid) {
            addFilehead(formData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.fileList=[];
              this.$modal.closeLoading();
              this.isSubmitDisabled=false;
            })
          }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除单词统计编号为"' + ids + '"的数据项？').then(function() {
        return delFilehead(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('datacenter/filehead/export', {
        ...this.queryParams
      }, `filehead_${new Date().getTime()}.xlsx`)
    },
    //上传
    beforeUpload(file,files) {
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1].toLowerCase();
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确，请上传${this.fileType.join("/")}格式文件!`);
          this.fileList=[]
          this.$refs.upload.clearFiles();
          return false;
        }
      }
      this.fileList[0]=file

      return true
    },
  }
};
</script>
<style scoped>
  .uploadImage{
    width: 100%;
    height: 100%;
  }
</style>
